#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "proto.h"

PUBLIC int ToWait(int WinIndex)
{
	MESSAGE temp;
	int NotNullMsg;
	NotNullMsg = LookTopMessage(&SysMessages, &temp);
	
	if (!NotNullMsg) return 1;
	if (temp.BelongsTo != WinIndex) return 1;

	return 0;
}

PUBLIC int GetMessage(struct MessageQueue *Source, MESSAGE *Store)
{
	if (Source->Head == Source->Tail)
	{
		 return 0;
	}
	
	*Store = Source->Queue[Source->Head];
	Source->Head = (Source->Head + 1) % MAX_MSG_BUFF;
	return 1;
}

PUBLIC int LookTopMessage(struct MessageQueue *Source, MESSAGE *Store)
{
	if (Source->Head == Source->Tail)
	{
		 return 0;
	}
	
	*Store = Source->Queue[Source->Head];
	return 1;
}

PUBLIC int SetTopMessage(struct MessageQueue *Source, MESSAGE Store)
{
	Source->Queue[Source->Head].BelongsTo = Store.BelongsTo;
	return 1;
}

PUBLIC void PutMessage(struct MessageQueue *Target, MESSAGE Source)
{
	Target->Queue[Target->Tail] = Source;
	Target->Tail = (Target->Tail + 1) % MAX_MSG_BUFF;
}

PUBLIC void InitMessageQueue(struct MessageQueue *Target)
{
	Target->Head = 0;
	Target->Tail = 0;
}
